package com.yanqu.road.server.cmd.activity.celebration;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.celebration.CelebrationProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.celebration.CelebrationMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

/**
 * @Description 庆典使用道具
 * @Author cwq
 * @Data 2021/7/12 16:08
 */
@Cmd(code = GameProtocol.S_CELEBRATION_USE_PROP, desc = "使用道具")
public class CelebrationUsePropCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CelebrationProto.CelebrationUsePropReqMsg reqMsg = CelebrationProto.CelebrationUsePropReqMsg.parseFrom(packet.getBytes());
        ret = CelebrationMgr.canEnterActivity(player,reqMsg.getActivityId());
        if(ret != 0){
            return;
        }
        if(reqMsg.getNum() <= 0){
            ret = GameErrorCode.E_BAG_USE_GOODS_COUNT_LESS_ONE;
            return;
        }
        if(!CelebrationMgr.getCelebrationActivityConfig().getPropList().contains(reqMsg.getPropId())){
            ret = GameErrorCode.E_BAG_ITEM_NO_EXIST;
            return;
        }
        if (reqMsg.getNum() > 1){
            if (VipMgr.getVipLv(player.getModule(PlayerModule.class).getUserInfo().getVipExp()) < GameConfig.COMMON_ACTIVITY_TEN_VIP_LIMIT
                    && player.getTitleId() < GameConfig.COMMON_ACTIVITY_TEN_TITLE_LIMIT) {
                ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;
                return;
            }
        }
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(reqMsg.getPropId());
        if(goodsInfo == null){
            ret = GameErrorCode.E_BAG_ITEM_NO_EXIST;
            return;
        }
        CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
        Property property = new Property(reqMsg.getPropId(), BigInteger.valueOf(reqMsg.getNum()));
        if(!currencyModule.currencyIsEnough(property)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        // 预扣除
        player.getModule(PreReduceModule.class).preReduce(property);
        player.sendPacket(CrossProtocol.C2_CELEBRATION_USE_PROP, reqMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            CelebrationProto.CelebrationUsePropRespMsg.Builder builder = CelebrationProto.CelebrationUsePropRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_CELEBRATION_USE_PROP, builder);
        }
    }
}
